package huixue.cn.com.rxapp.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.io.InputStream;

import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.util.LogUtils;

/**
 * Created by gongdongyang 2019/8/14
 * Describe:
 */
public class BigImageView2 extends View {

    private Bitmap mBitmap;

    private int mImageWidth = 0;
    private int mImageHeight = 0;

    private BitmapRegionDecoder mBitmapRegionDecoder;

    private Rect mRect = new Rect();
    private BitmapFactory.Options mOptions;


    public BigImageView2(Context context) {
        this(context,null);
    }

    public BigImageView2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public BigImageView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 指定图片的解码格式为RGB_565
        mOptions = new BitmapFactory.Options();
        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        LogUtils.d("BigImageView2","====onDraw===");
        //绘制图片
        if (mBitmapRegionDecoder != null) {

            checkHeight();
            checkWidth();

            LogUtils.d("BigImageView2","====mBitmapRegionDecoder===");
            // 加载图片的指定区域
            Bitmap bitmap = mBitmapRegionDecoder.decodeRegion(mRect, mOptions);
            canvas.drawBitmap(bitmap, 0, 0, null);
        }

    }

    public void setFile(InputStream inputStream){
        try{
            LogUtils.d("BigImageView2","====setFile===");
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(inputStream,null,options);

            //获取图片宽高
            mImageWidth = options.outWidth;
            mImageHeight = options.outHeight;

            mBitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);

            requestLayout();

        }catch (Exception e){

        }
    }

    private float lastX,lastY;
    private float currentX,currentY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //return super.onTouchEvent(event);
        int action = event.getAction();

        switch (action){
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                lastY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:{
                currentX = event.getX();
                currentY = event.getY();

                computeRect(lastX-currentX,lastY-currentY);
            }

                break;
            case MotionEvent.ACTION_UP:{
                currentX = event.getX();
                currentY = event.getY();
            }

            lastX = currentX;
            lastY = currentY;
        }

        return true;
    }


    private void computeRect(float distX,float distY){
        mRect.left = mRect.left + (int)distX;
        mRect.top = mRect.top + (int)distY;

        mRect.bottom = mRect.top+getHeight();
        mRect.right = mRect.left+getWidth();

        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取控件宽高
        int width = getMeasuredWidth();

        int height = getMeasuredHeight();

        mRect.left = (mImageWidth - width) / 2;
        mRect.right = mRect.left + width;
        mRect.top = (mImageHeight - height) / 2;
        mRect.bottom = mRect.top + height;
    }

    private void checkWidth() {
        // 只有当图片的宽大于控件的宽，控件横向显示不下图片时，才需要调整加载区域的左右位置
        if (mImageWidth > getWidth()) {
            if (mRect.right > mImageWidth) {
                mRect.right = mImageWidth;
                mRect.left = mRect.right - getWidth();
            }

            if (mRect.left < 0) {
                mRect.left = 0;
                mRect.right = getWidth();
            }
        } else {
            mRect.left = (mImageWidth - getWidth()) / 2;
            mRect.right = mRect.left + getWidth();
        }
    }

    /**
     * 检查加载区域是否超出图片范围
     */
    private void checkHeight() {
        // 只有当图片的高大于控件的高，控件纵向显示不下图片时，才需要调整加载区域的上下位置
        if (mImageHeight > getHeight()) {
            if (mRect.bottom > mImageHeight) {
                mRect.bottom = mImageHeight;
                mRect.top = mRect.bottom - getHeight();
            }
            if (mRect.top < 0) {
                mRect.top = 0;
                mRect.bottom = getHeight();
            }
        } else {
            mRect.top = (mImageHeight - getHeight()) / 2;
            mRect.bottom = mRect.top + getHeight();
        }
    }

}
